package com.core.util;

import java.util.ArrayList;
import java.util.List;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 字符串操作辅助类
 * 
 * 
 */
public class StringUtil {

	private static Log log = LogFactory.getLog(StringUtil.class);
	public static final String URL_ENCODE = "iso-8859-1";
	public static final String UTF_ENCODE = "utf-8";
	public static final String GBK_ENCODE = "gbk";
	public static final String GB2312_ENCODE = "gb2312";

	/**
	 * 将字符串截取转换为字符串ArrayList
	 * 
	 * @param chain
	 * @param split
	 * @return
	 */
	public static List<String> getListFromString(String chain, String split) {
		List<String> ret = null;
		try {
			if (chain == null || split == null) {
				return ret;
			}
			String[] chains = chain.split(split);
			if (chains == null || chains.length <= 0) {
				return ret;
			}
			ret = new ArrayList<String>();
			for (String string : chains) {
				ret.add(string);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

	/**
	 * 将字符串ArrayList转换为字符串
	 * 
	 * @param list
	 * @param split
	 * @return
	 */
	public static String getStringFromList(List<String> list, String split) {
		String ret = null;
		try {
			if (list == null || split == null) {
				return ret;
			}

			StringBuffer tmp = new StringBuffer();
			for (String string : list) {
				tmp.append(string).append(split);
			}
			ret = tmp.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

	/**
	 * 将对象数组转换为字符串对象
	 * 
	 * @param objects
	 * @param split
	 * @return
	 */
	public static String getStringFromArray(Object[] objects, String split) {
		String ret = null;
		try {
			if (objects == null || split == null) {
				return ret;
			}

			StringBuffer tmp = new StringBuffer();
			for (Object object : objects) {
				tmp.append(object).append(split);
			}
			ret = tmp.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

	/**
	 * 格式化多行文本转换为HTML显示效果
	 * 
	 * @param input
	 * @return
	 */
	public static String formatMultiLineString(String input) {
		String ret = null;
		try {
			if (input == null) {
				return ret;
			}
			ret = input.replaceAll("\n", "<br/>").replaceAll(" ", "&nbsp;");

		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

	/**
	 * 过滤字符串中的HTML标签
	 * 
	 * @param input
	 * @return
	 */
	public static String filterSplitString(String input) {
		String ret = null;
		try {
			ret = filterSplitString(input, 0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

	/**
	 * 过滤字符串中的HTML标签，并对过滤后的字符串进行截取操作
	 * 
	 * @param input
	 * @return
	 */
	public static String filterSplitString(String input, int length) {
		String ret = null;
		try {
			if (input == null) {
				return ret;
			}

			ret = input.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll(
					"<[^>]*>", "").replaceAll("[(/>)<]", "");

			if (length > 0) {
				if (ret.length() > length) {
					ret = ret.substring(0, length) + "...";
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

	/**
	 * 转换字符串编码
	 * 
	 * @param input
	 * @param encode
	 * @return
	 */
	public static String transferStringEncoding(String input, String encode) {
		String ret = null;
		try {
			if (input == null) {
				return ret;
			}
			if (encode == null || encode.equals("")) {
				encode = "GBK";
			}
			ret = new String(input.getBytes(URL_ENCODE), encode);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

	/**
	 * 加密字符串
	 * 
	 * @param input
	 * @return
	 */
	public static String encryptString(String input) {
		String ret = null;
		try {
			if (input == null) {
				return ret;
			}
			BASE64Encoder encoder = new BASE64Encoder();
			ret = encoder.encode(input.trim().getBytes()).trim();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

	/**
	 * 解密字符串
	 * 
	 * @param input
	 * @return
	 */
	public static String decryptString(String input) {
		String ret = null;
		try {
			if (input == null) {
				return ret;
			}

			BASE64Decoder decoder = new BASE64Decoder();
			ret = new String(decoder.decodeBuffer(input.trim()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

	/**
	 * 加密字符串，字节移动方式
	 * 
	 * @param input
	 * @return
	 */
	public static String encryptStringBymoveByte(String input) {
		String ret = "";
		try {
			if (input == null) {
				return ret;
			}
			byte[] bytes = input.trim().getBytes();
			for (int i = 0; i < bytes.length; i++) {
				ret = ret + (Integer.toHexString(bytes[i] & 0XFF));
			}

		} catch (Exception e) {
			ret = null;
			e.printStackTrace();
		}
		return ret;
	}

	/**
	 * 解密字符串，字节移动方式
	 * 
	 * @param input
	 * @return
	 */
	public static String decryptStringBymoveByte(String input) {
		String ret = "";
		try {
			if (input == null) {
				return ret;
			}
			input = input.trim();
			byte[] bytes = new byte[input.length() / 2];
			for (int i = 0; i < bytes.length; i++) {
				bytes[i] = Integer.valueOf(input.substring(i * 2, i * 2 + 2),
						16).byteValue();
			}
			ret = new String(bytes);

		} catch (Exception e) {
			ret = null;
			e.printStackTrace();
		}
		return ret;
	}
}
